package com.aihna.mall.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.aihna.common.idempotent.annotation.RepeatSubmit;
import com.aihna.common.log.annotation.Log;
import com.aihna.common.web.core.BaseController;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.common.core.domain.R;
import com.aihna.common.core.validate.AddGroup;
import com.aihna.common.core.validate.EditGroup;
import com.aihna.common.log.enums.BusinessType;
import com.aihna.common.excel.utils.ExcelUtil;
import com.aihna.mall.domain.vo.StoreUserIntegralRecordVo;
import com.aihna.mall.domain.bo.StoreUserIntegralRecordBo;
import com.aihna.mall.service.IStoreUserIntegralRecordService;
import com.aihna.common.mybatis.core.page.TableDataInfo;

/**
 * 用户积分记录
 *
 * @author Joseph.Chen
 * @date 2024-04-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/storeUserIntegralRecord")
public class StoreUserIntegralRecordController extends BaseController {

    private final IStoreUserIntegralRecordService storeUserIntegralRecordService;

    /**
     * 查询用户积分记录列表
     */
    @SaCheckPermission("mall:storeUserIntegralRecord:list")
    @GetMapping("/list")
    public TableDataInfo<StoreUserIntegralRecordVo> list(StoreUserIntegralRecordBo bo, PageQuery pageQuery) {
        return storeUserIntegralRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户积分记录列表
     */
    @SaCheckPermission("mall:storeUserIntegralRecord:export")
    @Log(title = "用户积分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StoreUserIntegralRecordBo bo, HttpServletResponse response) {
        List<StoreUserIntegralRecordVo> list = storeUserIntegralRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户积分记录", StoreUserIntegralRecordVo.class, response);
    }

    /**
     * 获取用户积分记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:storeUserIntegralRecord:query")
    @GetMapping("/{id}")
    public R<StoreUserIntegralRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(storeUserIntegralRecordService.queryById(id));
    }

    /**
     * 新增用户积分记录
     */
    @SaCheckPermission("mall:storeUserIntegralRecord:add")
    @Log(title = "用户积分记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StoreUserIntegralRecordBo bo) {
        return toAjax(storeUserIntegralRecordService.insertByBo(bo));
    }

    /**
     * 修改用户积分记录
     */
    @SaCheckPermission("mall:storeUserIntegralRecord:edit")
    @Log(title = "用户积分记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StoreUserIntegralRecordBo bo) {
        return toAjax(storeUserIntegralRecordService.updateByBo(bo));
    }

    /**
     * 删除用户积分记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:storeUserIntegralRecord:remove")
    @Log(title = "用户积分记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(storeUserIntegralRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
